package com.zhao.redis;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TestHash {

    private Jedis jedis;

    @Before
    public void before() {
        jedis = new Jedis("192.168.126.100", 6379);
    }

    @After
    public void after() {
        jedis.close();
    }

    @Test
    public void testZadd() {
        jedis.hset("zhangsan", "name", "zhngsan");
        jedis.hset("zhangsan", "age", "18");
        jedis.hset("zhangsan", "address", "beijing");
    }

    @Test
    public void testZget() {
        String name = jedis.hget("zhangsan", "name");
        String age = jedis.hget("zhangsan", "age");
        String address = jedis.hget("zhangsan", "address");
        System.out.println("name:" + name + "  age:" + age + "  address:" + address);
    }

    @Test
    public void testHgetall() {
        Map<String, String> zhangsan = jedis.hgetAll("zhangsan");
        zhangsan.forEach((key, value) -> System.out.println("key:" + key + " value:" + value));
    }

    @Test
    public void testHdel() {
        jedis.hdel("zhangsan", "address");
    }

    @Test
    public void testHexists() {
        boolean hexists = jedis.hexists("zhangsan", "address");
        System.out.println(hexists);
    }

    @Test
    public void testHkeys() {
        Set<String> zhangsan = jedis.hkeys("zhangsan");
        zhangsan.forEach(key -> System.out.println(key));
    }

    @Test
    public void testHvals() {
        List<String> zhangsan = jedis.hvals("zhangsan");
        zhangsan.forEach(key -> System.out.println(key));
    }

    @Test
    public void testHmset() {
        Map<String, String> lisi = new HashMap<>();
        lisi.put("name", "zhagnsan");
        lisi.put("age", "18");
        lisi.put("address", "hangzhou");
        jedis.hmset("lisi", lisi);
    }

    @Test
    public void testHmget() {
        List<String> hmget = jedis.hmget("lisi", "name", "age", "address");
        hmget.forEach(key -> System.out.println(key));
    }

    @Test
    public void testHsetnx() {
        jedis.hsetnx("lisi", "name", "lisi");
    }

    @Test
    public void testHincrby() {
        long l = jedis.hincrBy("lisi", "age", 2);
        System.out.println(l);
    }

    @Test
    public void testHincrbyfloat() {
        double l = jedis.hincrByFloat("lisi", "age", 2.4);
        System.out.println(l);
    }

}
